<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\plan\controller;

use app\common\controller\Admin;
use app\plan\controller\api\Plan as PlanApi;
use app\admin\controller\ajax\Library as Ajax;
use app\plan\controller\api\Script as ScriptApi;

/**
 * 政府预案管理
 * @package app\plan
 */
class Government  extends Admin

{
    
    protected $api;
    
	public function _initialize()
	{
		parent::_initialize();
		$this->api = new PlanApi();
           $this->ajax= new Ajax();
           $this->script=new ScriptApi();
	}
   /**
     * 首页
     * 
     */
    public function index() {
       
       
      if(IS_AJAX){
        $planList = $this->api->planList();
          
        return $planList;
      }

      $unitList = model('basic/unit')->column('fullName','id');
      $unitList[0]='管委会';
      $this->assign('unitList',$unitList);


      $type=$this->ajax->getplanType()->getData();
      $this->assign('type',$type);

      return $this->fetch();
  }

 	/**
 	 * 政府预案搜索
 	 */
 	public function search($typeId,$unitId) {
         
 		if(IS_AJAX){
			$planList = $this->api->search(input('typeId'),input('unitId'));
 			return $planList;
              
 		}
 	}
    /**
     * 添加政府预案
     * 
     */
    public function add() {
   		 if(IS_POST){
   		 	$data = input('post.');  
   		 	//有效性
                   if(isset($data['isValid'])){
                     $data['isValid'] = 1;
                     }else{
                       $data['isValid'] = 0;
                     }
                     $data['applyTime']=time();
                  return $this->api->add($data);
   		 }
           $unitList = model('basic/unit')->column('fullName','id');
           $unitList[0]='管委会';
           $this->assign('unitList',$unitList);
           
           $type=$this->ajax->getplanType()->getData();
           $this->assign('type',$type);
          
           return $this->fetch();    
     }
 
    /**
     * 修改政府预案
     * 
     */
    public function edit($planId = null) {
      
    	  if(IS_POST){
    	  	$data = input('post.');
                  //有效性
                       if(isset($data['isValid'])){
                       $data['isValid'] = 1;
                     }else{
                       $data['isValid'] = 0;
                     }
    	  	return $this->api->edit($data,$planId);
    	  }
           $unitList = model('basic/unit')->column('fullName','id');
           $unitList[0]='管委会';
           $this->assign('unitList',$unitList);

     	     $planList = $this->api->planList($planId)->getData();
           $type=$this->ajax->getplanType()->getData();
         
           $this->assign('type',$type);
           $this->assign('info',$planList['data'][0]);
           return $this->fetch();      
    }
    
    /**
     * 删除政府预案
     */
    public function del($planId = null) {
    	if(IS_AJAX){
    		return $this->api->del($planId);
    	}
    }


      /**
     * 政府预案步骤列表
     */
    public function scriptList($planId = null) {
        if(IS_AJAX){
        $scriptList = $this->script->scriptList($planId);
         return $scriptList;
      }

      $unitList = model('basic/unit')->column('fullName','id');
      $unitList[0]='管委会';
      $this->assign('unitList',$unitList);
      $this->assign('planId',$planId);
      return $this->fetch();
     }


     /**
     * 政府预案步骤添加
     */
    public function scriptAdd($planId = null) {

      if(empty($planId)){
        return 0;
      }
      if(IS_POST){
              $data = input('post.');  
              $data['reservePlanId']=$planId;
              return $this->script->add($data);
            }
      $unitList = model('basic/unit')->column('fullName','id');
      $unitList[0]='管委会';
      $this->assign('unitList',$unitList);
      $unitId = model('Plan')->where('id',$planId)->value('unitId');
      $this->assign('unitId',$unitId);
      $type=$this->ajax->getScriptType()->getData();
      $this->assign('planId',$planId);
      
      $this->assign('type',$type);
     return $this->fetch();    
     }


       /**
     * 修改企业预案步骤
     * 
     */
    public function scriptedit($planId = null,$scriptId = null) {
      
        if(IS_POST){
          $data = input('post.');
          $data['reservePlanId']=$planId;
          return $this->script->edit($data,$scriptId);
        }

        $scriptList = $this->script->scriptList($planId,$scriptId)->getData();

        $unitList = model('basic/unit')->column('fullName','id');
        $unitList[0]='管委会';
        $this->assign('unitList',$unitList);

        $type=$this->ajax->getScriptType()->getData();

         $this->assign('type',$type);
         $this->assign('info',$scriptList['data'][0]);
         $this->assign('planId',$planId);
         return $this->fetch();      
    }


        /**
     * 删除政府预案步骤
     */
    public function scriptdel($scriptId = null) {
      if(IS_AJAX){

        return $this->script->del($scriptId);
      }
    }

    /**
     * 申请审核
     * @param string $id
     */
    public function audit($id = null){
    	return $this->api->audit($id);
    }


  }



